package cm.example.shoppingplatformspringboot.mapper;

import cm.example.shoppingplatformspringboot.entity.Order;
import cm.example.shoppingplatformspringboot.entity.OrderItem;
import org.apache.ibatis.annotations.*;

import java.util.List;

@Mapper
public interface OrderMapper {
    // 插入订单
    @Insert("INSERT INTO wn_order (no, add_time, user_id, total_money, address, accept, telphone, pay_type, status) " +
            "VALUES (#{no}, #{add_time}, #{user_id}, #{total_money}, #{address}, #{accept}, #{telphone}, #{pay_type}, #{status})")
    @Options(useGeneratedKeys = true, keyProperty = "id")
    int insertOrder(Order order);

    // 插入商品订单下，下单的商品
    @Insert("INSERT INTO wn_order_item (order_id, goods_id, name, image, price, num) " +
            "VALUES (#{order_id}, #{goods_id}, #{name}, #{image}, #{price}, #{num})")
    int insertOrderItem(OrderItem item);

    // 查询单个订单（排除已删除的）
    @Select("SELECT * FROM wn_order WHERE id = #{order_id} AND status != '0'")
    Order selectOrderById(Integer order_id);

    //  根据订单编号查询订单
    @Select("SELECT * FROM wn_order WHERE no = #{no} AND status != '0'")
    List<Order> selectOrderByNo(String no);

    // 根据订单ID查询订单的商品列表
    @Select("SELECT * FROM wn_order_item WHERE order_id = #{order_id}")
    List<OrderItem> selectOrderItems(Integer order_id);

    //获取所有订单列表
    @Select("SELECT * FROM wn_order WHERE status != '0' ORDER BY add_time DESC")
    List<Order> selectAllOrders();

    //根据用户ID查询订单列表
    @Select("SELECT * FROM wn_order WHERE user_id = #{user_id} AND status != '0' ORDER BY add_time DESC")
    List<Order> selectOrderByUserId(Integer user_id);

    // 根据订单状态查询所有订单列表
    @Select("SELECT * FROM wn_order WHERE status = #{status} ORDER BY add_time DESC")
    List<Order> selectAllOrderByStatus(String status);

    //根据订单状态查询订单列表
    @Select("SELECT * FROM wn_order WHERE status = #{status} AND user_id = #{user_id} ORDER BY add_time DESC")
    List<Order> selectOrderByStatus(Integer user_id, String status);

    // 根据订单ID更新订单状态
    @Update("UPDATE wn_order SET status=#{status} WHERE id=#{id}")
    int updateOrderStatus(Integer id, String status);

    // 标记订单为已删除
    @Update("UPDATE wn_order SET status='0' WHERE id=#{id}")
    int markOrderAsDeleted(Integer id);

    // 更新订单地址方法（id为订单id）
    @Update("UPDATE wn_order SET accept=#{accept}, address=#{address}, telphone=#{telphone} WHERE id=#{id}")
    int updateOrderAddress(Order order);

    // 订单支付成功后更新订单支付时间
    @Update("UPDATE wn_order SET pay_time=NOW(), status = '2' " +
            "WHERE id=#{id} AND status = '1' AND pay_time IS NULL")
    int updateOrderPayTime(Integer id);

    // 更新订单发货时间
    @Update("UPDATE wn_order SET send_time=NOW(), status = '3' " +
            "WHERE id=#{orderId} AND status = '2' AND send_time IS NULL")
    int updateOrderSendTime(Integer orderId);

    // 更新订单完成时间
    @Update("UPDATE wn_order SET finish_time=NOW(), status = '4' " +
            "WHERE id=#{orderId} AND status = '3' AND finish_time IS NULL")
    int updateOrderCompleteTime(Integer orderId);
}